//
//  ViewController.m
//  UI19 - JSON(数据解析)
//
//  Created by 韩旭 on 16/8/9.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "HX_CollectionLayout.h"
#import "HX_CollectionViewCell.h"
#import "SelfSizeTools.h"
#import "ContentTitle.h"

@interface ViewController ()<HX_CollectionLayoutDelegate>
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) NSMutableArray<ContentTitle *> *arrTitle;
@end

@implementation ViewController

- (NSMutableArray *)arrTitle {
    if (!_arrTitle) {
        _arrTitle = [NSMutableArray array];
    }
    return _arrTitle;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    //  json 解析
    //    通过一个data ， 将JSON格式转换成OC认识的内容
    //    数组、字典
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"FirstJSON" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:dataPath];
    //    获取json解析
    //    json 解析
    NSError *error = nil;
    NSDictionary *dicJson = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    NSLog(@"%@", dicJson);
    NSDictionary *dic = dicJson[@"tabs"];
    NSArray *arr = dic[@"list"];
    for (NSDictionary *dic in arr) {
        [self.arrTitle addObject:[ContentTitle contentTitleDic:dic]];
    }
    HX_CollectionLayout *layout = (HX_CollectionLayout *)self.MyCollectionView.collectionViewLayout;
    layout.delegate = self;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout width:(CGFloat)width sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = [_arrTitle[indexPath.row] title];
//    return [SelfSizeTools selfSizeHeightWithString:title Width:50 Font:[UIFont systemFontOfSize:17]];
 CGRect new =  [title boundingRectWithSize:CGSizeMake(MAXFLOAT, CGRectGetHeight(self.MyCollectionView.bounds)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    return ceilf(CGRectGetWidth(new));
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrTitle.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HX_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.contentModel = _arrTitle[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
